package me.minecraft.plugin.pokemontip.config;

import com.cobblemon.mod.common.pokemon.Species;
import lg.minecraft.plugin.cobblemoncore.util.PokemonDataCache;
import lombok.Getter;
import me.minecraft.plugin.pokemontip.PokemonType;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
public class MainConfig {

    private final JavaPlugin plugin;
    private final File file;

    @Getter private List<String> blackWorldList;

    // 捕获相关配置
    @Getter private String captureMessage;
    @Getter private boolean captureEnable;

    @Getter private boolean captureNormal;
    @Getter private int captureIvNormal;
    @Getter private boolean captureShinyNormal;
    @Getter private int captureIvShinyNormal;
    @Getter private boolean captureMythical;
    @Getter private int captureIvMythical;
    @Getter private boolean captureShinyMythical;
    @Getter private int captureIvShinyMythical;
    @Getter private boolean captureUltraBeast;
    @Getter private int captureIvUltraBeast;
    @Getter private boolean captureShinyUltraBeast;
    @Getter private int captureIvShinyUltraBeast;
    @Getter private boolean captureLegendary;
    @Getter private int captureIvLegendary;
    @Getter private boolean captureShinyLegendary;
    @Getter private int captureIvShinyLegendary;
    @Getter private boolean captureParadox;
    @Getter private int captureIvParadox;
    @Getter private boolean captureShinyParadox;
    @Getter private int captureIvShinyParadox;

    @Getter private Map<Species, PokemonTypeRecord> captureTypeMap;

    // 生成相关配置
    @Getter private String spawnMessage;
    @Getter private String unknownPlayer;
    @Getter private boolean spawnEnable;
    @Getter private int spawnRange;

    @Getter private boolean spawnNormal;
    @Getter private int spawnIvNormal;
    @Getter private boolean spawnShinyNormal;
    @Getter private int spawnIvShinyNormal;
    @Getter private boolean spawnMythical;
    @Getter private int spawnIvMythical;
    @Getter private boolean spawnShinyMythical;
    @Getter private int spawnIvShinyMythical;
    @Getter private boolean spawnUltraBeast;
    @Getter private int spawnIvUltraBeast;
    @Getter private boolean spawnShinyUltraBeast;
    @Getter private int spawnIvShinyUltraBeast;
    @Getter private boolean spawnLegendary;
    @Getter private int spawnIvLegendary;
    @Getter private boolean spawnShinyLegendary;
    @Getter private int spawnIvShinyLegendary;
    @Getter private boolean spawnParadox;
    @Getter private int spawnIvParadox;
    @Getter private boolean spawnShinyParadox;
    @Getter private int spawnIvShinyParadox;

    @Getter private Map<Species, PokemonTypeRecord> spawnTypeMap;

    @Getter private String textName;
    @Getter private List<String> textHover;

    public MainConfig(JavaPlugin plugin) {
        this.plugin = plugin;
        this.file = new File(plugin.getDataFolder(), "config.yml");
        this.loadConfig();
    }

    public void loadConfig() {
        if (!file.exists()) plugin.saveResource("config.yml", false);

        FileConfiguration config = YamlConfiguration.loadConfiguration(file);

        // 初始化 PokemonType
        PokemonType.normal = new PokemonType(config.getString("type.normal"), 0);
        PokemonType.shiny_normal = new PokemonType(config.getString("type.shiny_normal"), 1);
        PokemonType.mythical = new PokemonType(config.getString("type.mythical"), 2);
        PokemonType.shiny_mythical = new PokemonType(config.getString("type.shiny_mythical"), 3);
        PokemonType.ultra_beast = new PokemonType(config.getString("type.ultra_beast"), 4);
        PokemonType.shiny_ultra_beast = new PokemonType(config.getString("type.shiny_ultra_beast"), 5);
        PokemonType.legendary = new PokemonType(config.getString("type.legendary"), 6);
        PokemonType.shiny_legendary = new PokemonType(config.getString("type.shiny_legendary"), 7);
        PokemonType.paradox = new PokemonType(config.getString("type.paradox"), 8);
        PokemonType.shiny_paradox = new PokemonType(config.getString("type.shiny_paradox"), 9);

        blackWorldList = config.getStringList("blackWorldList");

        // 加载捕获配置
        captureEnable = config.getBoolean("capture.enable");

        captureNormal = config.getBoolean("capture.normal");
        captureIvNormal = config.getInt("capture.iv_normal");
        captureShinyNormal = config.getBoolean("capture.shiny_normal");
        captureIvShinyNormal = config.getInt("capture.iv_shiny_normal");
        captureMythical = config.getBoolean("capture.mythical");
        captureIvMythical = config.getInt("capture.iv_mythical");
        captureShinyMythical = config.getBoolean("capture.shiny_mythical");
        captureIvShinyMythical = config.getInt("capture.iv_shiny_mythical");
        captureUltraBeast = config.getBoolean("capture.ultra_beast");
        captureIvUltraBeast = config.getInt("capture.iv_ultra_beast");
        captureShinyUltraBeast = config.getBoolean("capture.shiny_ultra_beast");
        captureIvShinyUltraBeast = config.getInt("capture.iv_shiny_ultra_beast");
        captureLegendary = config.getBoolean("capture.legendary");
        captureIvLegendary = config.getInt("capture.iv_legendary");
        captureShinyLegendary = config.getBoolean("capture.shiny_legendary");
        captureIvShinyLegendary = config.getInt("capture.iv_shiny_legendary");
        captureParadox = config.getBoolean("capture.paradox");
        captureIvParadox = config.getInt("capture.iv_paradox");
        captureShinyParadox = config.getBoolean("capture.shiny_paradox");
        captureIvShinyParadox = config.getInt("capture.iv_shiny_paradox");

        captureMessage = config.getString("capture.message");

        captureTypeMap = buildPokemonTypeMap(config.getStringList("capture.list"));

        // 加载生成配置
        spawnEnable = config.getBoolean("spawn.enable");

        spawnNormal = config.getBoolean("spawn.normal");
        spawnIvNormal = config.getInt("spawn.iv_normal");
        spawnShinyNormal = config.getBoolean("spawn.shiny_normal");
        spawnIvShinyNormal = config.getInt("spawn.iv_shiny_normal");
        spawnMythical = config.getBoolean("spawn.mythical");
        spawnIvMythical = config.getInt("spawn.iv_mythical");
        spawnShinyMythical = config.getBoolean("spawn.shiny_mythical");
        spawnIvShinyMythical = config.getInt("spawn.iv_shiny_mythical");
        spawnUltraBeast = config.getBoolean("spawn.ultra_beast");
        spawnIvUltraBeast = config.getInt("spawn.iv_ultra_beast");
        spawnShinyUltraBeast = config.getBoolean("spawn.shiny_ultra_beast");
        spawnIvShinyUltraBeast = config.getInt("spawn.iv_shiny_ultra_beast");
        spawnLegendary = config.getBoolean("spawn.legendary");
        spawnIvLegendary = config.getInt("spawn.iv_legendary");
        spawnShinyLegendary = config.getBoolean("spawn.shiny_legendary");
        spawnIvShinyLegendary = config.getInt("spawn.iv_shiny_legendary");
        spawnParadox = config.getBoolean("spawn.paradox");
        spawnIvParadox = config.getInt("spawn.iv_paradox");
        spawnShinyParadox = config.getBoolean("spawn.shiny_paradox");
        spawnIvShinyParadox = config.getInt("spawn.iv_shiny_paradox");

        spawnMessage = config.getString("spawn.message");
        unknownPlayer = config.getString("spawn.unknownPlayer");
        spawnRange = config.getInt("spawn.range");

        spawnTypeMap = buildPokemonTypeMap(config.getStringList("spawn.list"));

        textName = config.getString("text.name");
        textHover = config.getStringList("text.hover");
    }

    public Map<Species, PokemonTypeRecord> buildPokemonTypeMap(List<String> list) {
        Map<Species, PokemonTypeRecord> map = new HashMap<>();
        for (String txt : list) {
            String[] temp = txt.split("//", 3);
            if (temp.length != 3) {
                plugin.getLogger().warning("配置错误: " + txt);
                continue;
            }
            Species species = PokemonDataCache.getInstance().getSpeciesByKey(temp[0]);
            if (species == null) {
                plugin.getLogger().warning("配置错误: " + txt);
                continue;
            }
            int ivs;
            try {
                ivs = Integer.parseInt(temp[2]);
            } catch (NumberFormatException e) {
                plugin.getLogger().warning("配置错误: " + txt);
                continue;
            }
            map.put(species, new PokemonTypeRecord(temp[1], ivs));
        }
        return map;
    }

}
